<?php

namespace fanletter\model\element;

/**
 * 
 * LinkElement model
 * 
 * @author Evan
 *
 */
class LinkElement extends Element
{
	private $url = '';
	private $text = '';
	private $target = '';
	const DEFAULT_TEMPLATE_FILE = '';
	
	public function __construct($id = 0, $content = '', $order = 0, $contentId = 0)
	{
		parent::__construct($id, $content, $order, $contentId);
		if (!empty($content))
		{
			$this->fetchDataFromContent();
		}
	}
	
	public static function getDefaultTemplateFile()
	{
		return self::DEFAULT_TEMPLATE_FILE;
	}
	
	/**
	 * 
	 * Fetch all data from the XML-Content
	 */
	private function fetchDataFromContent()
	{
		try 
		{
			$xmlContent = new \SimpleXMLElement($this->content);
			$data = $xmlContent->children();
			$this->url = $data->url;
			$this->text = $data->text;
			$this->target = $data->target;
		}
		catch (\Exception $e)
		{
			echo $e->getMessage();
		}
	}
	
	public static function getType()
	{
		return __CLASS__;
	}
		
	public function getUrl()
	{
		return $this->url;
	}
	
	public function getText()
	{
		return $this->text;
	}
	
	public function getTarget()
	{
		return $this->target;
	}
	
	public function setUrl($url)
	{
		$this->url = (String)$url;
	}
	
	public function setText($text)
	{
		$this->text = (String)$text;
	}
	
	public function setTarget($target)
	{
		$this->target = (String)$target;
	}
	
	/**
	 * returns all the Element-Arguments
	 * ELEMENT_URL = URL of the link
	 * ELEMENT_TEXT = Text of the link
	 * ELEMENT_TARGET = Target of the link
	 * 
	 * @return Array
	 */
	public function getBuilderArguments()
	{
		$arguments['ELEMENT_URL'] = $this->url;
		$arguments['ELEMENT_TEXT'] = $this->text;
		$arguments['ELEMENT_TARGET'] = $this->target;
		
		return $arguments;
	}
	
	/**
	 * returns Content in XML-String
	 * 
	 * @see Element::getContent()
	 * @return String
	 */
	public function getContent()
	{
		$linkElement = new \SimpleXMLElement('<LinkElement></LinkElement>');
		$linkElement->addChild('url', $this->url);
		$linkElement->addChild('text', $this->text);
		$linkElement->addChild('target', $this->target);
		return $linkElement->asXML();
	}
	
}
